<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Auth;

class User extends Authenticatable
{


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email','type', 'password'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['is_current_user', 'type_label'];


    /**
     * get default avatar
     *
     * @return string
     */
    public function getAvatar()
    {
        return '/assets/admin/img/user2-160x160.jpg';
    }

    /**
     * get user by email
     *
     * @param $email
     * @return mixed
     */

    public static function getUserByEmail($email)
    {
        return  self::where('email',$email)->with('canteen')->first();
    }

    /**
     * check user is current user function
     *
     * @return bool
     */
    public function isCurrentUser()
    {
        $currentUser_id = Auth::id();
        return $currentUser_id == $this->id;
    }

    /**
     * Get the administrator flag for the user.
     *
     * @return bool
     */
    public function getIsCurrentUserAttribute()
    {
        return $this->isCurrentUser();
    }
    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getActivatedAttribute($value)
    {
        return intval($value);
    }

    public function isAdmin()
    {
        return $this->type == 1;
    }

    public function isManager()
    {
        return $this->type == 0;
    }

    public function getTypeLabelAttribute()
    {
        $type_labels = ['库存管理员', '系统管理员'];
        return $type_labels[$this->type];
    }

    public function canteen()
    {
        return $this->hasOne(Canteen::class);
    }
    
}



